package com.zzk.lanouhn.myapplication1.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestPingLunAuthorPhotoAdapter;
import com.zzk.lanouhn.myapplication1.modles.InvestPingLunAuthorPhotoModle;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/19.
 */
public class InvestPinglunAuthorPhoto extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ImageView iv_fanhui;

    private InvestPingLunAuthorPhotoAdapter adapter;
    private InvestPingLunAuthorPhotoModle photoModle;

    private String photoID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest_pinglun_author_photo);
        recyclerView = (RecyclerView) findViewById(R.id.pinglun_author_photo_xrecyclerView);
        iv_fanhui = (ImageView) findViewById(R.id.pinglun_author_photo_fanhui);

        photoID = getIntent().getStringExtra("photo");

        GridLayoutManager glm = new GridLayoutManager(InvestPinglunAuthorPhoto.this, 3);
        glm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(glm);

        getPhotoData();
        inivData();
    }

    private void inivData() {
        iv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getPhotoData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://user.zhangyoubao.com/apis/rest/UsersService/getOtherUserPhotos?" +
                        "other_userid=" + photoID + "&userid=19316729&token=780662dcfb78c86be20ed34abd0e4e20de4&i_=" +
                        "K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&deviceId=" +
                        "K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&t_=1474273521163&p_=5834&v_=" +
                        "400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=22&cha=baiduMartket&u_=19316729&modle";
                OkHttpClient client = new OkHttpClient();
                Request reqest = new Request.Builder().url(url).build();
                Call call = client.newCall(reqest);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();
                        photoModle = gson.fromJson(ss, InvestPingLunAuthorPhotoModle.class);
                        if (photoModle.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });

            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    adapter = new InvestPingLunAuthorPhotoAdapter(InvestPinglunAuthorPhoto.this, photoModle);
                    recyclerView.setAdapter(adapter);
                    break;
            }
        }
    };

}
